package com.xcoder.leetcode;

/**
 * https://leetcode.cn/problems/diao-zheng-shu-zu-shun-xu-shi-qi-shu-wei-yu-ou-shu-qian-mian-lcof/
 */
public class 剑指Offer_21_数组使奇数在前偶数在后 {

    public static void main(String[] args) {
        int[] nums = {2, 5, 33, 6, 9, 82, 1};
        int[] nums2 = {1, 5, 34, 6, 9, 81, 12, 53};
        exchange(nums2);
        for (int i : nums2) {
            System.out.print(i + " ");
        }
    }

    public static int[] exchange(int[] nums) {
        int i = 0, j = nums.length - 1;
        while (i < j) {
            while (i < j && (nums[i] & 1) == 1) {
                i++;
            }

            while (i < j && (nums[j] & 1) == 0) {
                j--;
            }

            int tmp = nums[i];
            nums[i] = nums[j];
            nums[j] = tmp;
        }
        return nums;
    }

}
